package com.isecommerce.framework.entity;

import com.isecommerce.framework.util.*;
import java.util.*;

/**
 * Comma Seperate Value Entity
 *
 *
 *
 * @version 1.2.5
 * @author Leslie, Chen
 */
// Leslie [2004.04.29]: Created.

public class CSVEntity extends Entity{

    protected ArrayList<String> arr;
    protected int count;
    protected String delimiter;

    /**
     * Constructor for given CSV string.
     *
     * @param csvData CSV string.
     */
    public CSVEntity(String csvData){
    	new CSVEntity(csvData, ",");        
    }
    
    public CSVEntity(String csvData, String delimiter){
    	this.delimiter = delimiter;
    	this.setCSVData(csvData);
    }

    /**
     * Making a dummy CSV entity by given item count.
     *
     * @param itemCount Number of fields.
     */
    public CSVEntity(int itemCount){
    	new CSVEntity(itemCount, ",");
    }
    
    /**
     * Making a dummy CSV entity by given item count.
     *
     * @param itemCount Number of fields.
     */
    public CSVEntity(int itemCount, String delimiter){
    	this.delimiter = delimiter;
        this.arr = new ArrayList<String>();

        for(int i=0; i<itemCount; i++)
            arr.add("");
    }

    /**
     * Set new CSV data with given string.
     *
     * @param csvData Given CSV string.
     */
    public void setCSVData(String csvData){
        this.arr = new ArrayList<String>();

        if(csvData.length() == 0) return;

        //csvData = Utility.replaceAll(csvData, "\\,", "*~.~*");

        Tokenizer token = new Tokenizer(csvData, this.delimiter);

        while(token.hasMoreTokens()){
            //this.arr.add(Utility.replaceAll(token.nextToken(),"*~.~*",","));
        	this.arr.add(token.nextToken());
        }
    }

    /**
     * Get the number of fields in currrent CSV string.
     *
     * @return Number of fields.
     */
    public int getItemCount(){
        return arr.size();
    }

    /**
     * Get value from CSV string by given column.
     *
     * @param col Column number.
     * @return Value string.
     */
    public String getItemAt(int col){
        if(col >= arr.size()) return null;

        return (String) arr.get(col);
    }

    /**
     * Set value at CSV string with given column.
     *
     * @param col Given column.
     * @param value Value string.
     */
    public void setItem(int col, String value){
        if(col >= arr.size()) return;

        arr.remove(col);
        arr.add(col, value);
    }

    /**
     * Convert CSV entity to one line CSV string.
     *
     * @return CSV string.
     */
    public String toString(){
        boolean isFirst = true;
        StringBuffer buf = new StringBuffer();
        String str = "";

        if(arr.size() == 0) return "";
        
        //buf.append("[CSV_ENTITY](" + String.valueOf(this.getItemCount()) + "): ");
        
        for(int i=0; i<arr.size(); i++){
            //str = Utility.replaceAll((String) arr.get(i), ",", "\\,");
        	str = (String) arr.get(i);
        	
            if(isFirst) {
                buf.append(str);
                isFirst = false;
            }else buf.append(delimiter + str);
        }

        return buf.toString();
    }
}
